package vtt.android.cartoon_video.dialogs;

import vtt.android.cartoon_video.BaseApplication;
import vtt.android.cartoon_video.R;
import vtt.android.library.utils.VttLog;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class DialogManager {
	
    private VttLog log = new VttLog("DialogManager", BaseApplication.isDebuggable);
    
	//private static HashMap<Integer, Integer> dialogTypeMap = new HashMap<Integer, Integer>();
	public static final int DIALOG_TYPE_INFOR = 1;
	public static final int DIALOG_TYPE_WARNING = 2;
	public static final int DIALOG_TYPE_CONFIRM = 3;
	public static final int DIALOG_TYPE_ERROR = 4;
	
	
	private Context mContext;
	private long startTime = 0;

	public DialogManager(Context ctx) {
		this.mContext = ctx;
	}

	/**
	 * show common dialog
	 * @param dlType
	 * @param title
	 * @param message
	 * @param listener
	 * @return Dialog
	 */
	public Dialog showCommonDialog(int dlType, String title, String message, final IDialogListener listener) {
		
		if(System.currentTimeMillis() - startTime < 1000){
			startTime = System.currentTimeMillis();
			return null;
		}
		startTime = System.currentTimeMillis();
		
		final Dialog dialog = new Dialog(mContext, R.style.VttThemeDialogTransparent);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_common);
		dialog.setCanceledOnTouchOutside(false);
		
		// Textview
		TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
		TextView tvMessage = (TextView) dialog.findViewById(R.id.tv_message);
		
		if(title != null && !title.isEmpty()){
			tvTitle.setText(title);
		}
		
		if(message != null && !message.isEmpty()){
			tvMessage.setText(message);
		}
		
		// Button 
		Button btnOK = (Button) dialog.findViewById(R.id.btn_yes);
		Button btnNo = (Button) dialog.findViewById(R.id.btn_no);
		
		btnOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if(listener != null){
					listener.onOkClick();
				}
			}
		});
		
		if(dlType == DIALOG_TYPE_CONFIRM){
			// set text color blue if dialog is confirm
			tvMessage.setTextColor(mContext.getResources().getColor(R.color.holo_blue_dark));
			tvMessage.setGravity(Gravity.LEFT);
			
			btnNo.setVisibility(View.VISIBLE);
			btnNo.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
					if(listener != null){
						listener.onCancelClick();
					}
				}
			});
		}
		
		// show it
		dialog.show();
		return dialog;
	}
	
	public Dialog showAnimationSettingDialog(int checkedItem, final IListDialogListener listener){
	    
	    if(System.currentTimeMillis() - startTime < 1000){
            startTime = System.currentTimeMillis();
            return null;
        }
        startTime = System.currentTimeMillis();
        
	    final String[] animationList = mContext.getResources().getStringArray(R.array.lps_jazzy_effects);
	    
	    final Dialog dialog = new Dialog(mContext, R.style.VttThemeDialogTransparent);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_setting_animation);
        
        ListView lvSetting = (ListView) dialog.findViewById(R.id.dialog_setting_listview); 
        
        lvSetting.setAdapter(new ArrayAdapter<String>(mContext, 
                android.R.layout.simple_list_item_single_choice,
                animationList));
        lvSetting.setItemsCanFocus(true);
        lvSetting.setItemChecked(checkedItem, true);
        
        lvSetting.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(listener != null){
                    listener.onItemClicked(position, animationList[position]);
                }
                dialog.dismiss();
            }
        });
        
        dialog.show();
        return dialog;
	}
	
	public interface IDialogListener {
		public void onOkClick();
		public void onCancelClick();
	}

	public interface IListDialogListener {
        public void onItemClicked(int position, String itemName);
    }
}
